package com.jwiki.app.jtoolkit.controller;

import java.sql.SQLException;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.jwiki.app.jtoolkit.common.db.JdbcFactory;
import com.jwiki.app.jtoolkit.controller.data.Response;
import com.jwiki.app.jtoolkit.controller.form.GetColumnForm;

@RestController
@RequestMapping("/mybatis")
public class MyBatisController extends Controller {

    @RequestMapping("/getTable")
    public Response getTable(@RequestBody GetColumnForm form) throws SQLException {
        JdbcFactory jdbc = new JdbcFactory();
        String url = String.format("jdbc:mysql://%s:%d/%s?characterEncoding=utf8&useSSL=true", form.getHost(),
                form.getPort(), form.getDatabase());
        jdbc.setConnectConfig(url, form.getUser(), form.getPassword());
        return json(jdbc.getTable(form.getTable()));
    }

}
